/**
 * Created by linxiaofen on 2017/5/11.
 */
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var open = require('open');

var app = {
    srcPath:'src/',
    devPath:'build/',
    prdPath:'dist/'
};

//第三方工具
gulp.task('lib',function () {
    gulp.src( app.srcPath + 'vendor/**/*')
        .pipe(gulp.dest(app.devPath + 'vendor'))
        .pipe(gulp.dest(app.prdPath + 'vendor'))
        .pipe($.connect.reload())
});

//html
gulp.task('html',function(){
    gulp.src(app.srcPath + 'main/**/*.html')
        .pipe(gulp.dest(app.devPath))
        .pipe(gulp.dest(app.prdPath))
});

//样式
gulp.task('less',function(){
    gulp.src(app.srcPath + 'main/style/index.less')
        .pipe($.less())
        .pipe(gulp.dest(app.devPath + 'css'))
        .pipe($.cssmin())
        .pipe(gulp.dest(app.prdPath + 'css'))
        .pipe($.connect.reload())
});

//js
gulp.task('js',function () {
    gulp.src(app.srcPath + 'main/script/**/*.js')
        .pipe($.concat('index.js'))
        .pipe(gulp.dest(app.devPath + 'js'))
        .pipe($.uglify())
        .pipe(gulp.dest(app.prdPath + 'js'))
        .pipe($.connect.reload())
});

//照片处理
gulp.task('images',function () {
    gulp.src(app.srcPath + 'main/images/**/*')
        .pipe(gulp.dest(app.devPath + 'images'))
        .pipe($.imagemin())
        .pipe(gulp.dest(app.prdPath + 'images'))
        .pipe($.connect.reload())
});


//数据
gulp.task('json',function () {
    gulp.src(app.srcPath + 'main/data/**/*.json')
        .pipe(gulp.dest(app.devPath + 'data'))
        .pipe(gulp.dest(app.prdPath + 'data'))
        .pipe($.connect.reload())
});

gulp.task('build',['lib','html','less','js','images']);

gulp.task('clean',function () {
    gulp.src([app.devPath,app.prdPath])
        .pipe($.clean());
});

gulp.task('server',['build'],function () {
    $.connect.server({
        root:[app.devPath],
        livereload:true,
        port:7081
    });

    open('http://localhost:7081');
    gulp.watch(app.srcPath + 'vendor/**/*.js',['lib']);
    gulp.watch(app.srcPath + 'main/**/*.html',['html']);
    gulp.watch(app.srcPath + 'main/data/**/*.json',['json']);
    gulp.watch(app.srcPath + 'main/style/**/*.less',['less']);
    gulp.watch(app.srcPath + 'main/script/**/*.js',['js']);
    gulp.watch(app.srcPath + 'main/images/**/*',['images']);
});

gulp.task('default',['server']);
